﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MicropleSoft.WmsApp.Library;
using MicropleSoft.WmsApp.Library.ApplicationCache;
using MicropleSoft.WmsApp.Library.LookUpInterface;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.Controls.ComplexControl
{
    public partial class CLookUpEdit : CBaseComplexControl, IComplexDataBinder, IDataSource, ILookUpViewHeaderBuilder
    {
        public event LookUpEditValueChangedEventHandler LookUpEditValueChanged = null;
        public CLookUpEdit()
        {
            InitializeComponent();
            if (GlobalConfig.RunTimeMode)
            {
                this.xLookUpEdit.EditValueChanged += new EventHandler(XLookUpEditEditValueChanged);
            }
        }

        private void XLookUpEditEditValueChanged(object sender, EventArgs e)
        {
            if (LookUpEditValueChanged != null)
            {
                if (this.xLookUpEdit.GetSelectedDataRow() is DataRowView)
                {
                    LookUpEditValueChanged((DataRowView)this.xLookUpEdit.GetSelectedDataRow());
                }
            }
        }

        public void ExcuteDataBinding(WmsApp.Library.IBaseDomainObject baseDomainObject)
        {
            this.xLookUpEdit.EditValueChanged -= new EventHandler(XLookUpEditEditValueChanged);
            this.xLookUpEdit.ExcuteDataBinding(baseDomainObject);
            this.xLookUpEdit.EditValueChanged += new EventHandler(XLookUpEditEditValueChanged);
        }

        public bool IsPrimaryKey
        {
            get
            {
                return xLookUpEdit.IsPrimaryKey;
            }
            set
            {
                this.xLookUpEdit.IsPrimaryKey = value;
            }
        }

        public bool ReadOnly
        {
            get
            {
                return this.xLookUpEdit.ReadOnly;
            }
            set
            {
                this.xLookUpEdit.ReadOnly = value;
            }
        }

        public string PropertyName
        {
            get
            {
                return this.xLookUpEdit.PropertyName;
            }
            set
            {
                this.xLookUpEdit.PropertyName = value;
            }
        }

        public object EditValue
        {
            get
            {
                return this.xLookUpEdit.EditValue;
            }
            set
            {
                this.xLookUpEdit.EditValue = value;
            }
        }

        #region Implementation of IDataSource

        /// <summary>
        ///  数据源ID
        /// </summary>
        public string DataSourceKey
        {
            get { return this.xLookUpEdit.DataSourceKey; }
            set { this.xLookUpEdit.DataSourceKey = value; }
        }

        /// <summary>
        /// 数据源是否存于缓存
        /// </summary>
        public bool DataSourceUseCache
        {
            get { return this.xLookUpEdit.DataSourceUseCache; }
            set { this.xLookUpEdit.DataSourceUseCache = value; }
        }

        /// <summary>
        /// 缓存对象
        /// </summary>
        /// <returns>IBaseDataProvider</returns>
        public IBaseDataProvider GetBaseDataProvider()
        {
            return this.xLookUpEdit.GetBaseDataProvider();
        }

        /// <summary>
        /// 设置数据源
        /// </summary>
        /// <param name="data"></param>
        public void SetDataSource(DataTable data)
        {
           this.xLookUpEdit.SetDataSource(data);
        }

        #endregion

        #region Implementation of ILookUpViewHeaderBuilder

        public void BuildColumns(List<LookUpViewHeader> lookUpViewHeaders)
        {
           this.xLookUpEdit.BuildColumns(lookUpViewHeaders);
        }

        #endregion
    }
}
